package Cliente;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;


public class JanelaChat extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextArea mensagens;
	private JTextField inputMessage;
	//private JLabel usersOnline;
	private String myUser;
	private JButton sendButton;
	private JList onlineUsersList;
	private String messageFromUser;
	private boolean closingCommand;
	private DefaultListModel listModel;
	private boolean newMsg;
	

	public JanelaChat(String title) throws HeadlessException {
		super(title);
		// TODO Auto-generated constructor stub
	}

	public void mostraJanela() {
		setSize(new Dimension(800,500));
		setMinimumSize(new Dimension(100, 50));
		setBounds(400, 200, 800, 500);
		setResizable(false);
		setLocation(100, 200);
		closingCommand = false;
		newMsg = false;
		listModel = new DefaultListModel();
		
		inputMessage = new JTextField(50);
		inputMessage.setSize(new Dimension(500, 10));
		inputMessage.setFont(new Font("Arial", Font.PLAIN, 12));
		inputMessage.setPreferredSize(new Dimension(400, 15));
		inputMessage.addActionListener(this);
		
		mensagens = new JTextArea();
		mensagens.setColumns(30);
		mensagens.setRows(50);
		mensagens.setPreferredSize(new Dimension(600, 500));
		mensagens.setEditable(false);
		mensagens.setWrapStyleWord(true);
		mensagens.setAutoscrolls(true);
		mensagens.setLineWrap(true);

		JScrollPane scrollPane = new JScrollPane(mensagens);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setPreferredSize(new Dimension(600, 200));
		
		//String[] users = onlineUsers.split(",");
		
		onlineUsersList = new JList(listModel);
		onlineUsersList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		onlineUsersList.setLayoutOrientation(JList.VERTICAL);
		onlineUsersList.setLayoutOrientation(JList.VERTICAL);
		onlineUsersList.setVisibleRowCount(-1);
		onlineUsersList.setPreferredSize(new Dimension(10,500));
		
        JScrollPane listScroller = new JScrollPane(onlineUsersList);
        listScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        listScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        listScroller.setPreferredSize(new Dimension(10, 200));
        listScroller.setAlignmentX(RIGHT_ALIGNMENT);

		
		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		JLabel label = new JLabel("Seja bem-vindo ao nosso chat, " + myUser + "! Utilize a lista a sua direita para falar com uma pessoa ou com um grupo de pessoas em especial a qualquer momento.");
		
		sendButton = new JButton("Enviar");
		sendButton.addActionListener(this);
		
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addComponent(label)
				.addGroup(layout.createParallelGroup(Alignment.LEADING)
					.addComponent(scrollPane)
					.addComponent(listScroller))
				.addGroup(layout.createParallelGroup(Alignment.LEADING)
					.addComponent(inputMessage)
					.addComponent(sendButton)));
		
		layout.setHorizontalGroup(layout.createParallelGroup()
				.addComponent(label)
				.addGroup(layout.createParallelGroup(Alignment.LEADING))
					.addGroup(layout.createSequentialGroup()
						.addComponent(scrollPane)
						.addComponent(listScroller))
					.addGroup(layout.createSequentialGroup()
						.addComponent(inputMessage)
						.addComponent(sendButton)));

		
		pack();
		//setDefaultCloseOperation(EXIT_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
			     onExit();
			   }

		});
		setVisible(true);
	}

	public String getMensagensChat() {
		return mensagens.getText();
	}

	public void setMensagensChat(String message) {
		this.mensagens.append(message + "\n");
	}

	public String getNick(String message) {
		myUser = (String)JOptionPane.showInputDialog(this,message, "Chat" ,JOptionPane.QUESTION_MESSAGE);
		if(myUser == null)
			System.exit(0);
		return myUser;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
			Object[] selected = onlineUsersList.getSelectedValues();
			String recipients = new String("");
			for(Object obj : selected){
				recipients+= "@"+(String)obj+" ";
			}
			onlineUsersList.clearSelection();
			setMessageFromUser(recipients+inputMessage.getText());
			setNewMsg(true);
			inputMessage.setText("");
	}

	public String getMyUser() {
		return myUser;
	}

	public void setMyUser(String myUser) {
		this.myUser = myUser;
	}

	public String getMessageFromUser() {
		if(messageFromUser!=null){
			String msg = new String(messageFromUser);
			messageFromUser = null;
			return msg;
		}
		return null;
	}

	public void setMessageFromUser(String messageFromUser) {
		this.messageFromUser = messageFromUser;
	}
	
	public void setClosingCommand(boolean closingCommand) {
		this.closingCommand = closingCommand;
	}

	public boolean getClosingCommand() {
		return closingCommand;
	}

	public void onExit(){
		closingCommand=true;
	}

	public void addToList(String newUser){
		listModel.addElement(newUser);
	}
	
	public void removeFromList(String user){
		listModel.removeElement(user);
	}
	
	public void setList(String onlineUsers){
		for(String s : onlineUsers.split(";")){
			if(!s.equals(myUser))
			listModel.addElement(s);
		}
	}

	public void setNewMsg(boolean newMsg) {
		this.newMsg = newMsg;
	}

	public boolean getNewMsg() {
		return newMsg;
	}
	
}
